package com.it.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.it.reggie.common.BaseContext;
import com.it.reggie.common.Result;
import com.it.reggie.entity.ShoppingCart;
import com.it.reggie.service.IShoppingCartService;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author:cyh
 * @data:2022/12/18 23:11
 */
@Api(tags = "购物车管理")
@Slf4j
@RestController
@RequestMapping("/shoppingCart")
public class ShoppingCartController {
    @Autowired
    IShoppingCartService shoppingCartService;


    /**
     * 查询购物车
     * @return
     */
    @GetMapping("/list")
    public Result<List<ShoppingCart>> querty(){
        LambdaQueryWrapper<ShoppingCart> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(ShoppingCart::getUserId, BaseContext.getCurrentId());
        wrapper.orderByAsc(ShoppingCart::getCreateTime);
        List<ShoppingCart> list = shoppingCartService.list(wrapper);
        return Result.success(list);
    }

    /**
     * 添加购物车
     * @param shoppingCart
     * @return
     */
    @PostMapping("/add")
    public Result<ShoppingCart> save(@RequestBody ShoppingCart shoppingCart){
        //获取当前登录的用户id
        Long userId = BaseContext.getCurrentId();
        LambdaUpdateWrapper<ShoppingCart> wrapper = new LambdaUpdateWrapper<>();
        wrapper.eq(ShoppingCart::getUserId, userId);
        //判断当前用户点的时菜品还是套餐
        if (shoppingCart.getDishId()!=null){//说明点的时菜品
            wrapper.eq(ShoppingCart::getDishId,shoppingCart.getDishId());
        }else {//说明点的是套餐
            wrapper.eq(ShoppingCart::getSetmealId, shoppingCart.getSetmealId());
        }
        //根据用户id和点的餐查询数据库
        ShoppingCart one = shoppingCartService.getOne(wrapper);
        shoppingCart.setUserId(userId);
        if (one==null){//数据库不存在,则新增
            shoppingCart.setNumber(1);
            shoppingCartService.save(shoppingCart);
        }else {//说明数据库存在,修改该菜品或分类的数量
            one.setNumber(one.getNumber()+1);
            shoppingCartService.updateById(one);
        }
        return Result.success(shoppingCart);
    }

    /**
     * 根据id删除购物车
     * @param shoppingCart
     * @return
     */
    @PostMapping("/sub")
    public Result<ShoppingCart> deleteById(@RequestBody ShoppingCart shoppingCart){
        //获取当前登录的用户id
        Long userId = BaseContext.getCurrentId();
        LambdaQueryWrapper<ShoppingCart> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(ShoppingCart::getUserId,userId);
        //判断删除的是菜品还是套餐
        if (shoppingCart.getDishId()!=null){//说明是菜品
            wrapper.eq(ShoppingCart::getDishId,shoppingCart.getDishId());
        }else {//说明是套餐
            wrapper.eq(ShoppingCart::getSetmealId,shoppingCart.getSetmealId());
        }
        //根据条件查询信息,如果number>1,减一,如果number==1,删除
        ShoppingCart one = shoppingCartService.getOne(wrapper);
        Integer number = one.getNumber();
        one.setNumber(number-1);
        if (one.getNumber()>0){
            //one.setNumber(one.getNumber()-1);
            shoppingCartService.updateById(one);
        }else {
            shoppingCartService.remove(wrapper);
        }
        return Result.success(one);
    }

    /**
     * 清空购物车
     * @return
     */
    @DeleteMapping("/clean")
    public Result removeAll(){
        shoppingCartService.removeById(BaseContext.getCurrentId());
        return Result.success("清空购物车成功");
    }
}
